
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Config;
using Swaf.Container;

namespace Approval
{
	public enum CompanyApprovalState
	{
		Disable=0,
		Enable=1,
		NotSet=2
	}
	public enum ApprovalActionState
	{
		ForApproval=0,
		Approve=1,
		Denied=2,
		Rollback=3,
		NoApproval=4
	}
	public class ApprovalConfigPartner : IConfigPartner,IApprovalManager
	{
		protected IBizObjList m_approvalInfos;
		public ApprovalConfigPartner(IApplication app)
		{

		}

		public void checkConfigStatus()
		{
			foreach (IBizObj approvalInfo in ApprovalInfos)
				foreach (IBizObj approvalInfoRule in (IBizObjList)approvalInfo.get("rules"))
					approvalInfoRule.put("decorator",
						DecoratorFactory.createFromNickName(approvalInfoRule.get("factoryName").ToString(), approvalInfoRule, approvalInfo));
			
		}

		public IClassFactory DecoratorFactory { get { return Application.globalApp.factories["ApprovalDecorators"]; } }
		public virtual IBizObjList ApprovalInfos
		{
			get
			{
				if (m_approvalInfos == null)
					m_approvalInfos = Application.globalApp.bizObjMgr.createList("ApprovalInfo");
				return m_approvalInfos;

			}
		}
		public void registerForConfig(IConfigManager config)
		{
			config.registerHandler("...<ApprovalInfoSetup>", new ApprovalConfigHandler(this));
			config.registerHandler("...<ApprovalInfoSetup> <Rule>", new RuleConfigHandler(this));
			
		}
		private class RuleConfigHandler : ConfigHandler
		{
			protected static string[] s_legalAttrib ={ "ruleName", "factoryName","setCall" };
			protected ApprovalConfigPartner m_partner;
			public RuleConfigHandler(ApprovalConfigPartner partner){m_partner = partner;}
			public override void executeBefore(IConfigElement configInfo)
			{
				configInfo.checkIllegalAttributes(s_legalAttrib);
				IBizObj approvalInfo = configInfo.Context["ApprovalInfo"] as IBizObj;
				Debug.Assert(approvalInfo != null);
				IBizObjList rules = approvalInfo.get("rules") as IBizObjList;
				IBizObj newRule = rules.insertNew();
				newRule.put("ruleName", configInfo.getAttribute("ruleName"));
				newRule.put("factoryName", configInfo.getAttribute("factoryName"));
				newRule.put("setCall", configInfo.getAttribute("setCall"));
			}
		}

		private class ApprovalConfigHandler : ConfigHandler
		{
			protected static string[] s_legalAttrib = new string[] { "id", "parentFieldId","set","setFieldId","master","subs" };
			protected ApprovalConfigPartner m_partner;
			public ApprovalConfigHandler(ApprovalConfigPartner partner) { m_partner = partner; }
			public override void executeBefore(IConfigElement configInfo)
			{
				configInfo.checkIllegalAttributes(s_legalAttrib);
				IBizObj approvalInfo = Application.globalApp.bizObjMgr.create("ApprovalInfo");
				approvalInfo.put("id", configInfo.getAttribute("id"));
				approvalInfo.put("parentFieldId", configInfo.getAttribute("parentFieldId"));
				approvalInfo.put("set", configInfo.getAttribute("set"));
				approvalInfo.put("master", configInfo.getAttribute("master"));
				approvalInfo.put("subs", configInfo.getAttribute("subs"));
				configInfo.Context["ApprovalInfo"] = approvalInfo;
				m_partner.ApprovalInfos.Add(approvalInfo);
			}
		}


		
	}

	public interface IApprovalDecorator : Swaf.BizRule.IRuleDecorator
	{

	}
	public interface IApprovalManager
	{
		IBizObjList ApprovalInfos { get;}
	}
}
